package pack4;

class Solution9 {
    public ListNode oddEvenList(ListNode head) {
        if (head == null) return null;

        ListNode odd = head;
        ListNode even = head.next;
        ListNode evenHead = even;

        while (even != null && even.next != null) {
            odd.next = even.next;
            odd = odd.next;
            even.next = odd.next;
            even = even.next;
        }

        odd.next = evenHead;
        return head;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);

        Solution9 solution = new Solution9();
        ListNode result = solution.oddEvenList(head);
        System.out.print("["+result.val);
        result = result.next;
        while (result != null) {
            System.out.print(",");
            System.out.print(result.val);
            result = result.next;
        }
        System.out.print("]");
    }
}

class ListNode9 {
    int val;
    ListNode next;
    ListNode9(int x) { val = x; }
}